package com.yuke.cloud.service.omc.utils;

import cn.hutool.core.date.DateField;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/*
 * <p>Company: Copyright (c) 2018. yuke All Rights Reserved. </br> </p>
 * <p>Title：yukemall </br> </p>
 * <p>com.yuke.cloud.service.omc.utils Description TODO </br> </p>
 * @Author wg
 * @Date   2019/10/6 12:13
 */

/**
 * The class Date util.
 *
 * @author
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class DateUtil {

    /**
     * 获取系统前时间.
     *
     * @param minute the minute
     *
     * @return the before time[yyyy-MM-dd HH:mm:ss]
     */
    public static String getBeforeTime(int minute) {
        Date newDate = cn.hutool.core.date.DateUtil.offset(new Date(), DateField.MINUTE, -minute);
        return cn.hutool.core.date.DateUtil.formatDateTime(newDate);
    }

    // 获取当天0点时间戳 Created by Wenhui Huang 2019/02/28
    public static Long dayTimeInMillis() {
        Calendar calendar = Calendar.getInstance();// 获取当前日期
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Long time = calendar.getTimeInMillis();
        return time;
    }

    // 获取当月0点时间戳 Created by Wenhui Huang 2019/02/28
    public static Long monthTimeBeginInMillis(Date date) {
        Calendar calendar = Calendar.getInstance();// 获取当前日期
        Long dateTimeMillis = date.getTime();
        calendar.setTime(new Date(dateTimeMillis));
        calendar.add(Calendar.YEAR, 0);
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Long time = calendar.getTimeInMillis();
        return time;
    }

    // 获取当月23.59.59时间戳
    public static Long monthTimeEndInMillis(Date date) {
        Calendar calendar = Calendar.getInstance();
        Long dateTimeMillis = date.getTime();
        calendar.setTime(new Date(dateTimeMillis));
        calendar.set(calendar.get(Calendar.YEAR),
        calendar.get(Calendar.MONDAY),
        calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        Long time = calendar.getTimeInMillis();
        return time;
    }

    public static Date monthTimeBegin(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }
    public static Date monthTimeEnd(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONDAY), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return calendar.getTime();
    }


    // 获取当年0点时间戳 Created by Wenhui Huang 2019/02/28
    public static Long yearTimeInMillis() {
        Calendar calendar = Calendar.getInstance();// 获取当前日期
        calendar.add(Calendar.YEAR, 0);
        calendar.add(Calendar.DATE, 0);
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Long time = calendar.getTimeInMillis();
        return time;
    }

    public static Date lastMonthTimeBegin(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());

        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);

        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static Date lastMonthTimeEnd(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.DATE, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        //
        return calendar.getTime();
    }

    public static String timeStamp2Date(String seconds,String format) {
       if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
            return "";
          }
        if(format == null || format.isEmpty()){
               format = "yyyy-MM-dd HH:mm:ss";
          }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds+"000")));
    }

    public static String getStringDate(Date currentTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        return dateString;
      }

    public static Date getDateString(String currentTime) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dateString = formatter.parse(currentTime);
        return dateString;
    }
    public static Date nDaysAfter(Date nowDate ,Integer n){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(nowDate);
        calendar.add(Calendar.DATE,+n);
        return calendar.getTime();
    }
}
